export const requestUrl = "http://127.0.0.1:7001";
// export const requestUrl = "http://42.193.231.115";
const requestAddress = requestUrl + "/api/wx";
export const requestUploadUrl = requestUrl + "/api/admin/upload";
const token = uni.getStorageSync("userInfo")?.token;
interface Data<T> {
    data: T,
    success: boolean,
    error?: string,
    msg?: string
}
// 类型参数
export default <T>(url: string, data = {}, method: "GET" | "POST" | "DELETE" = "GET") => {
    return new Promise<Data<T>>((resolve, reject) => {
        uni.request({
            url: requestAddress + url, method,
            data,
            header: { token },
            success: res => {
                const status = res.statusCode;
                switch (status) {
                    case 200:
                        return resolve(res.data as Data<T>)
                    case 404:
                        return reject(404)
                    case 401:
                        uni.navigateTo({ url: "pages/login/index" })
                        return reject(401)
                    case 500:
                    case 501:
                    case 503:
                        uni.showToast({ title: "出现异常错误", icon: "none" })
                        return reject("服务器异常")
                    default:
                        uni.showToast({ title: (res.data as Data<T>).error, icon: "none" })
                        return reject(status)
                }
            },
            fail: (err: any) => {
                uni.hideLoading()
                uni.showToast({ title: "发生错误", icon: "none" })
                console.log(err);
                
                reject(err)
            }
        })
    })
}